package rs.mbs2.epics.server.servlet.util;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

import rs.mbs2.epics.server.entity.User;

public class Emailer {

	private static String emailSubjectVerification = "Some user activity";

	public Emailer() {
		
	}

	public void sendMessage(Session session, String from, String to) throws AddressException, MessagingException {

		// Constructs the message 
		javax.mail.Message msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress(from));
		msg.setRecipients(RecipientType.TO, InternetAddress.parse(to));
		msg.setSubject("New activity on your account");
		msg.setText("There is some new activity on your account, please check it out.");
		msg.setSentDate(new Date());
		
		// Sends the message
		Transport.send(msg);

		System.out.println("MESSAGE BEAN: Mail was sent successfully.");
	}
	
	public boolean sendEMail(User recepient) {

		String host = "smtp.gmail.com";
		String port = "587";
		String eMailFrom = "mbs2testmail@gmail.com";
		final String emailUName = "mbs2testmail@gmail.com";
		final String emailPass = "mbs2test";
		final String senderSignature = "EPics project";

		Properties properties = System.getProperties();
		// Setup mail server and session
		properties.setProperty("mail.smtp.host", host);
		properties.put("mail.smtp.port", port);
		properties.put("mail.smtp.from", eMailFrom);
		properties.put("mail.smtp.auth", "true");
		properties.put("mail.smtp.starttls.enable", "true");
		properties.put("mail.debug", "false");
		/*
		properties.put("mail.smtp.socketFactory.port", port);
		properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		properties.put("mail.smtp.socketFactory.fallback", "true");
		try {
			MailSSLSocketFactory sf = new MailSSLSocketFactory();
			  sf.setTrustAllHosts(true);
			  properties.put("mail.smtp.ssl.socketFactory", sf);
		} catch (GeneralSecurityException e1) {
			e1.printStackTrace();
		}*/
		properties.setProperty("mail.transport.protocol", "smtp");

		Session mailSession = Session.getDefaultInstance(properties,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(emailUName, emailPass);
					}
				});
		mailSession.setDebug(true);

		// message creation
		try {
			MimeMessage msg = new MimeMessage(mailSession);
			msg.setFrom(new InternetAddress(eMailFrom));
			msg.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(recepient.getEmailAddress()));
				msg.setSubject(emailSubjectVerification);
				String contentMsg = String.format("Dear "+ recepient.getFirstname() + " " + recepient.getLastname() + "<br/>"
								+ "there has been some activity on your photos, please check it out."
								+ "<br/><br/> Best regards!<br/><br/>"+ senderSignature);
				msg.setText(contentMsg, "UTF-8", "html");

			Transport.send(msg); // send email
			System.out.println("email message was sent successfully !");
			return true;
		} catch (MessagingException e) {
			e.printStackTrace();
			return false;
		}

	}
}
